/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sirtet.tetris.generator;

import java.util.ArrayList;
import java.util.List;
import sirtet.tetris.TetrisPiece;
import sirtet.tetris.TetrisPieceFactory;

/**
 * This class provides static methods related to piece generating.
 * @author guangwei.zhu
 */
public final class TetrisPieceGenerator extends TetrisPieceFactory {

    /**
     * Transform a piece represented as BlokusPiece into an instance of
     * TetrisPiece.
     * @param piece the piece to be transformed
     * @return the same piece represented as a TetrisPiece
     */
    public static TetrisPiece fromBlokusPiece(BlokusPiece piece) {
        RawTetrisPiece tp;
        int row, col, size;
        int i, j;
        Object[][] data;

        if (piece == null) {
            return null;
        }

        row = piece.squares.length;
        col = piece.squares[0].length;
        size = 0;

        for (i = 0; i < row; i++) {
            for (j = 0; j < col; j++) {
                if (piece.squares[i][j]) {
                    size++;
                }
            }
        }

        tp = new RawTetrisPiece(size);
        data = tp.exposeData();

        for (i = 0; i < row; i++) {
            for (j = 0; j < col; j++) {
                if (piece.squares[i][j]) {
                    data[i][j] = TetrisPiece.NORMAL;
                } else {
                    data[i][j] = TetrisPiece.EMPTY;
                }
            }
        }
        tp.align();

        return (TetrisPiece) tp;
    }
    
    /**
     * Enumerate all pieces with specified number of squares
     * @param numofsquare The number of squares in each piece
     * @return A list of generated TetrisPieces
     */
    public static List<TetrisPiece> enumerate(int numofsquare) {
        PieceList pList = new PieceList();
        ArrayList<TetrisPiece> pieces = new ArrayList<TetrisPiece>();
        TetrisPiece pPiece;
        
        // Generator Pieces
        pList.setNumberSquare(numofsquare);
        
        for(BlokusPiece bPiece : pList.getPieces())
        {
            pPiece = TetrisPieceGenerator.fromBlokusPiece(bPiece);
            pPiece.normalize();
            pieces.add(pPiece);
        }
        
        return pieces;
    }

    // Make class static only
    private TetrisPieceGenerator() {}
}

